home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / Samples / Moofwars 1.02 / Tim's Libraries / TGraphic.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-05  |  6.7 KB  |  193 lines  |  [TEXT/CWIE]

  1. /*****************************************************************************
  2. TGraphic.h
  3.  
  4. Author: Timothy Carroll
  5. Apple Developer Technical Support
  6. timc@apple.com
  7.  
  8. Copyright © 1996, 1997 Apple Computer, Inc., All Rights Reserved
  9.  
  10. You may incorporate this sample code into your applications without
  11. restriction, though the sample code has been provided "AS IS" and the
  12. responsibility for its operation is 100% yours.  However, what you are
  13. not permitted to do is to redistribute the source as "DSC Sample Code"
  14. after having made changes. If you're going to re-distribute the source,
  15. we require that you make it clear in the source that the code was
  16. descended from Apple Sample Code, but that you've made changes.
  17.  
  18. See TGraphic.cp for description and the list of changes.
  19.  
  20. *************************************************************************************/
  21.  
  22.  
  23. #ifndef _TGRAPHIC_
  24. #define _TGRAPHIC_
  25.  
  26. #pragma once
  27.  
  28. #include <QDOffscreen.h>
  29. #include "Error Macros.h"
  30.  
  31.  
  32. #if PRAGMA_STRUCT_ALIGN
  33. #pragma options align=power
  34. #endif
  35.  
  36. enum eDrawFlag
  37. {
  38.     kDrawGraphic = 0,
  39.     kDrawBackground = 1
  40. };
  41.  
  42. // We declare the app color table here, must be defined in the app's code
  43. extern CTabHandle gAppColorTable;
  44.  
  45. class TGraphic
  46.     {
  47.     public:
  48. /*************************************************************************************
  49. Static Creator and Reference Counting
  50.     
  51. These are the routines that handle the actual creation of the objects, along
  52. with reference counting and so on.
  53. *************************************************************************************/
  54.  
  55.     static TGraphic         *NewGraphic (SInt16 resID);
  56.  
  57.     void AddReference (void);
  58.     void DisposeReference (void);
  59.  
  60. /*************************************************************************************
  61. Locking and Unlocking
  62.     
  63. You are never required to lock a graphic -- the routines run fine either way. These 
  64. functions are provided because most of the time, all of the graphics will be allocated
  65. ahead of time, and locking them high means more memory for the app, and less handles
  66. to move around.
  67. *************************************************************************************/
  68.     OSStatus    LockGraphic (void);
  69.     OSStatus    UnlockGraphic (void);
  70.     
  71.     
  72. /*************************************************************************************
  73. Creating and destroying the graphic
  74.     
  75. CreateGraphic loads the resource and processes it to the internal graphic format.
  76. DestroyGraphic disposes of any internal data allocated for a TGraphic.
  77. *************************************************************************************/
  78.     OSStatus    CreateGraphic (void);
  79.     OSStatus    DestroyGraphic (void);
  80.  
  81.  
  82. /*************************************************************************************
  83. File Output
  84.     
  85. These routines allow convenient writing of a graphic out to a resource fork, 
  86. in either the compressed format or as a PICT resource.
  87. *************************************************************************************/
  88.  
  89.     OSStatus    WriteToGraphicResource (void);
  90.     OSStatus    WriteToPICTResource (void);
  91.     
  92. /*************************************************************************************
  93. Accessors Functions
  94.  
  95. These are inlined for speed, most other information about the sprite is private and
  96. subject to change.
  97. *************************************************************************************/
  98.  
  99.     SInt16        GetResID(void) {return fResID;}
  100.     Rect        GetBounds (void) {return fBounds;}        
  101.  
  102. /*************************************************************************************
  103. Core Functions
  104.  
  105. Here are the primary functions of TGraphic, methods to draw or hit test!
  106.     
  107. CopyImage accept a point in the local coordinate system of the destination PixMap.
  108. If the "useBackground" flag is set and a background PixMap was specified, it will draw
  109. from the background to the destination, using the graphic's data only as a mask.
  110. This allows a cheap and easy "erase" function.
  111.     
  112. HitTest accepts a point in coordinates local to the TGraphic's bounds rect, and returns
  113. true if that point hits a "drawn" portion of the graphic.
  114.     
  115. Intersect is a static function that accepts two TGraphic objects and the top left
  116. coordinate for each object.  Both points should be in the same coordinate system. It 
  117. returns true if the masks intersect at any point.
  118. *************************************************************************************/
  119.     
  120.     void            CopyImage (SInt32 top, SInt32 left, Boolean useBackground);
  121.     Boolean            HitTest (SInt32 v, SInt32 h);
  122.     static Boolean    Intersect (TGraphic *object1, TGraphic *object2,
  123.                                 SInt32 v1, SInt32 h1, SInt32 v2, SInt32 h2);
  124.     
  125.     
  126.     protected:
  127.  
  128.  
  129. /*************************************************************************************
  130. Internal Functions
  131.  
  132. We have a number of routines that can load from various resource formats, along with
  133. the actual sprite encoder.
  134.  
  135. We also have a number of routines that actually get called to do the blitting.
  136. *************************************************************************************/
  137.  
  138.     OSStatus    LoadFromGraphicResource (void);
  139.     OSStatus    LoadFromPICTResource (void);
  140.     OSStatus    LoadFromICN8Resource (void);
  141.     OSStatus    LoadFromCIconResource (void);
  142.     
  143.     OSStatus    EncodeGraphic (PixMapHandle theGraphic, Rect *encodeRect);
  144.  
  145.     void GraphicClipped (SInt32 top, SInt32 left, UInt32 clipLeft, UInt32 clipRight, UInt32 clipTop, UInt32 clipBottom);
  146.     void GraphicUnclipped (SInt32 top, SInt32 left);
  147.     void BackgroundClipped (SInt32 top, SInt32 left, UInt32 clipLeft, UInt32 clipRight, UInt32 clipTop, UInt32 clipBottom);
  148.     void BackgroundUnclipped (SInt32 top, SInt32 left);
  149.  
  150.     // There are mostly useful for dumping to a PICT file, so we don't have clipped
  151.     // versions of these, and the functionality isn't exposed to outside clients.
  152.     
  153.     void DrawMaskUnclipped (SInt32 top, SInt32 left);
  154.     void HitMaskUnclipped (SInt32 top, SInt32 left);
  155.  
  156. /*************************************************************************************
  157. Constructor/Destructor
  158.  
  159. The constructor and destructor shouldn't be called directly.  These routines are
  160. called automatically when a new sprite is created by the static routine, and when
  161. all references are released.
  162. *************************************************************************************/
  163.  
  164.     TGraphic (SInt16 resID);
  165.     ~TGraphic(void);
  166.  
  167.  
  168. /*************************************************************************************
  169. Data Structures
  170.  
  171. I force alignment to PPC, but the alignment would be the same on 68K.    
  172. *************************************************************************************/
  173.  
  174.     SInt16            fResID;
  175.     UInt16            fReferenceCount;
  176.         
  177.     UInt16            fBitDepth;
  178.     UInt16            fFlags;
  179.  
  180.     Rect            fBounds;
  181.  
  182. // This handle holds the data the TGraphic class uses to draw itself
  183.     Handle            fImage;
  184.     
  185. // This handle holds the data the TGraphic class uses for hit testing
  186.     Handle            fHitMask;    
  187. };
  188.  
  189. #if PRAGMA_STRUCT_ALIGN
  190. #pragma options align=reset
  191. #endif
  192.  
  193. #endif /* _TGRAPHIC_ */